﻿using BaoVeBienCuong.Playgame.army;
using System;
using System.Collections.Generic;
using BaoVeBienCuong.SelectMap;
using System.IO;
using System.IO.IsolatedStorage;

namespace BaoVeBienCuong.Store
{
    class FileTuong
    {
        static string temp;
        static IsolatedStorageFile storage;
        public static void getData(ref List<LinhCanChien> linhs)
        {
            storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (!storage.FileExists("sunnet_l12sq_MuaTuong.txt"))
            {
                using (var writer = new StreamWriter(
                new IsolatedStorageFileStream("sunnet_l12sq_MuaTuong.txt", FileMode.Create, FileAccess.Write, storage)))
                {
                    writer.WriteLine("0;0");
                    writer.WriteLine("1;0");
                    writer.WriteLine("2;1");
                    writer.WriteLine("3;0");
                    writer.WriteLine("4;0");
                    writer.WriteLine("5;1");
                    writer.WriteLine("6;0");
                    writer.WriteLine("7;0");
                    writer.WriteLine("8;1");
                    writer.WriteLine("9;0");
                    writer.WriteLine("10;1");
                    writer.WriteLine("11;1");
                    writer.Flush();
                    writer.Close();
                    writer.Dispose();
                }
            }
            IsolatedStorageFileStream f = storage.OpenFile("sunnet_l12sq_MuaTuong.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
            using (StreamReader r = new StreamReader(f))
            {
                do
                {
                    temp = r.ReadLine();
                    string[] result = temp.Split(';');
                    bool t = false;
                    if (result[1].Equals("0")) t = false;
                    else t = true;
                    linhs[Int16.Parse(result[0])].setLock(t);
                } while (!r.EndOfStream);
                r.Close();
                r.Dispose();
            }//mở file ra để đọc
            storage.Dispose();
        }
        public static void SaveFile(ref List<LinhCanChien> linhs, int Idtuong)
        {
            linhs[Idtuong].setLock(false);
            storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (storage.FileExists("sunnet_l12sq_MuaTuong.txt"))
            {
                storage.DeleteFile("sunnet_l12sq_MuaTuong.txt");
            }
            using (var writer = new StreamWriter(
                new IsolatedStorageFileStream("sunnet_l12sq_MuaTuong.txt", FileMode.Create, FileAccess.Write, storage)))
            {
                for (int t = 0; t < 12; t++)
                {
                    writer.WriteLine(t + ";" + (linhs[t].getLock() == true ? "1" : "0"));
                }
                writer.Flush();
                writer.Close();
                writer.Dispose();
            }
            storage.Dispose();
        }
        public static bool getBought(int index)
        {
            storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (!storage.FileExists("sunnet_l12sq_MuaTuong.txt"))
            {
                using (var writer = new StreamWriter(
                new IsolatedStorageFileStream("sunnet_l12sq_MuaTuong.txt", FileMode.Create, FileAccess.Write, storage)))
                {
                    writer.WriteLine("0;0");
                    writer.WriteLine("1;0");
                    writer.WriteLine("2;1");
                    writer.WriteLine("3;0");
                    writer.WriteLine("4;0");
                    writer.WriteLine("5;1");
                    writer.WriteLine("6;0");
                    writer.WriteLine("7;0");
                    writer.WriteLine("8;1");
                    writer.WriteLine("9;0");
                    writer.WriteLine("10;1");
                    writer.WriteLine("11;1");
                    writer.Flush();
                    writer.Close();
                    writer.Dispose();
                }
            }
            IsolatedStorageFileStream f = storage.OpenFile("sunnet_l12sq_MuaTuong.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
            int i = 0;
            bool t = false;
            using (StreamReader r = new StreamReader(f))
            {
                do
                {
                    temp = r.ReadLine();
                    string[] result = temp.Split(';');
                    if (result[1].Equals("0")) t = false;
                    else t = true;
                    if (i == index) break;
                    i++;
                } while (!r.EndOfStream);
                r.Close();
                r.Dispose();
            }//mở file ra để đọc
            storage.Dispose();
            return t;
        }
    }
}
